<?php
/***************************************************************************
 *   copyright : (C) 2009 Udrea Cristian
 *   site : http://code.google.com/p/testauction-php
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as 
 *   published by the Free Software Foundation; either version 2 of the
 *   License, or (at your option) any later version. Although none of the
 *   code may be sold. If you have been sold this script, get a refund.
 ***************************************************************************/
include(APP_ROOT.'include/classes/constants.php');
include('../library/functions.php');
#=======CONSTANTS=======
$ROWSPERPAGE = 10;
#=======================

if(!isset($productID)) {
	$sql = "SELECT auction_id, product_id, closed_timestamp, base_price,"
		."offer_increment FROM ".TBL_AUCTIONS ." WHERE closed = 1";
}
else {
	$sql = "SELECT auction_id, product_id, closed_timestamp, base_price,"
		."offer_increment FROM ".TBL_AUCTIONS ." WHERE closed = 1 AND product_id = $productID";
	$prod_info = execute_query('SELECT * FROM '.TBL_PRODUCTS.' WHERE product_id = ?', array($productID));
}

$result = execute_query($sql);
$pagingLink = getPagingLink($sql, $ROWSPERPAGE);
?>
<p>&nbsp;</p>
 <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
   <tr align="center" class="listTableHeader">
	 <td colspan="7" class="text"><form name="form" action="index.php" method="get">
	   <br />
	   Numele produsului&nbsp;
	   <input type="hidden" name="view" id="view" value="search"/>
	   <input type="hidden" name="find" id="find" value="closed"/>
	   <input type="text" class="box" name="q" id="q" />
	   <input type="submit" class="box" name="submit" id="submit" value="Cauta" />
	 </form></td>
   </tr>
   <tr align="center" class="listTableHeader"> 
     <td>Nume Produs</td>
     <td>Foto</td>
	 <td>Data Inchiderii</td>
     <td>Castigator</td>
     <td>Stare</td>
	 <td>Actiuni</td>
   </tr>
<?php
if(count($result) > 0) {
	for($i = 0; $i < count($result); $i++) {
		if ($i%2) {
			$class = 'row1';
		} else {
			$class = 'row2';
		}
		if(!isset($productID)) {
			$prod_info = execute_query('SELECT * FROM '. TBL_PRODUCTS .' WHERE product_id = ?', array($result[$i]['product_id']));
		}
		$state_text = "Neconfirmat";
		$win_text = "";
		$temp_r = execute_query('SELECT userkey FROM '. TBL_OFFERS .' WHERE auction_id = ? ORDER BY timestamp DESC', array($result[$i]['auction_id']));
		$userkey = $temp_r[0]['userkey'];
		$n_items = count($temp_r);
		if($n_items > 0) {
			$temp_r = execute_query('SELECT username FROM '. TBL_USERS .' WHERE userkey = ?', array($userkey));
			$win_text = $temp_r[0]['username'] ."<br />". ($result[$i]['base_price'] + $result[$i]['offer_increment'] * $n_items) ." CREDITS";
		}
		else {
			$win_text = "<a href=\"javascript:renewAuction(" . $result[$i]['auction_id'] . ");\">Se reporteaza</a>";
			$state_text = "Nu au existat ofertanti.";
		}
		$temp_r = execute_query('SELECT od_claim_type, od_claimed, od_status FROM '. TBL_ORDERS . ' WHERE auction_id = ? AND od_shipping_userkey = ?',
			array($result[$i]['auction_id'], $userkey));
		$n_items = count($temp_r);
		if($n_items == 1) {
			$temp_r = $temp_r[0];
			if($temp_r['od_claimed'] == "Y") {
				$state_text = "";
				if($temp_r['od_claim_type'] == "Accepted") {
					$state_text .= "<img style=\"border: 0;\" alt=\"Acceptat\" title=\"Acceptat\" src=\"../include/accepted.png\"/>&nbsp;";
				}
				else {
					$state_text .= "<img style=\"border: 0;\" alt=\"Refuzat\" title=\"Refuzat\" src=\"../include/refused.png\"/>";
				}
				switch($temp_r['od_status']) {
					case "New" :
						$state_text .= "<img style=\"border: 0;\" alt=\"Nou\" title=\"Nou\" src=\"../include/new.png\"/>";
						break;
					case "Paid" :
						$state_text .= "<img style=\"border: 0;\" alt=\"Platit\" title=\"Platit\" src=\"../include/paid.png\"/>";
						break;
					case "Shipped" :
						$state_text .= "<img style=\"border: 0;\" alt=\"Trimis\" title=\"Trimis\" src=\"../include/shipped.png\"/>";
						break;
					case "Completed" :
						$state_text .= "<img style=\"border: 0;\" alt=\"Complet\" title=\"Complet\" src=\"../include/complete.gif\"/>";
						break;
					case "Cancelled" :
						$state_text .= "<img style=\"border: 0;\" alt=\"Anulat\" title=\"Anulat\" src=\"../include/cancel.png\"/>";
						break;
				}
			}
		}
		else {
			if($n_items > 0) {
				$state_text = "Exista mai multe facturi ptr aceasta licitatie.<br />"
					."Se recomanda suspendarea licitatiei si verificarea ofertelor!<br />"
					."In cazul unei erori recente se recomanda stergerea licitatiei.";
			}
		}
		?>
		<tr class="<?php echo $class; ?>"> 
		   <td><a href="../product/index.php?view=detail&productId=<?php echo $result[$i]['product_id']; ?>">
				<?php echo $prod_info[0]['product_name']; ?></a></td>
		   <td width="75" align="center"><img src="../../produse/thumb_<?php echo $prod_info[0]['pict1_url']; ?>"/></td>
		   <td align="center">
			<?php echo date("j/n/Y - H:i:s", $result[$i]['closed_timestamp']); ?>
		   </td>
		   <td align="center">
			<?php echo $win_text; ?>
		   </td>
		   <td align="center">
			<?php echo $state_text; ?>
		   </td>
		   <td align="center">
			<a href="javascript:deleteAuction(<?php echo $result[$i]['auction_id']; ?>, 'closed');"><img style="border: 0;" alt="Sterge" title="Sterge" src="../include/delete.png"/>
		   </a></td>
		</tr>
<?php
	}
?>
  <tr><td colspan="7" align="center"><?php echo $pagingLink; ?></td></tr>
<?php 
}
else {
?>
  <tr><td colspan="7" align="center">Nu au fost adaugate licitatii</td></tr>
<?php
}
?>
  <tr><td colspan="7">&nbsp;</td></tr>
 </table>
 <p>&nbsp;</p>
